import matplotlib.pyplot as plt
import numpy as np
import os

def get_a_file_data(filename, need_key):
    with open(filename, 'r') as f:
        row_list = f.read().splitlines()
    
    for row in row_list:
        key, value = row.split(':')
        if key == need_key:
            return eval(value)

def cr(cr_data,ave_us,worst_us):
    x_data_old = ['MID', 'EDF', 'SEDF', 'SRPT', 'FB', 'THR', 'OPTFB', 'RM', 'SRPT+EDF', 'SRPT-A', 'FIFO', 'FIFO+EDF']
    cr_data_old = cr_data
    data_dict = {}
    plt.figure(figsize=(6, 4))
    for i in range(len(x_data_old)):
        data_dict[x_data_old[i]] = cr_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    cr_data = [data_dict[label] for label in x_data]
    plt.bar(x_data,cr_data,color="deepskyblue",label="AveCR")

    #line
    ave_us_data_old = ave_us
    data_dict = {}
    for i in range(len(ave_us_data_old)):
        data_dict[x_data_old[i]] = ave_us_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_us_data = [data_dict[label] for label in x_data]
    plt.plot(x_data, ave_us_data, color = "sandybrown", marker='>', ms=6, label="AveCRUR", linewidth=3)
    

    worst_us_data_old = worst_us
    data_dict = {}
    for i in range(len(worst_us_data_old)):
        data_dict[x_data_old[i]] = worst_us_data_old[i]
    x_data =  ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    worst_us_data = [data_dict[label] for label in x_data]
    plt.plot(x_data, worst_us_data, color = "crimson", marker='o', ms=6, linestyle='-.', label="WorstCRUR", linewidth=3)


    # ave_Comp_ratio_old = ave_vsr
    # data_dict = {}
    # for i in range(len(ave_Comp_ratio_old)):
    #     data_dict[x_data_old[i]] = ave_Comp_ratio_old[i]
    # x_data =  ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    # ave_Comp_ratio = [data_dict[label] for label in x_data]
    # plt.plot(x_data, ave_Comp_ratio, color = "plum", marker='*', ms=6, label="AveACRC", linewidth=3) # valid slot ratio 


    # worst_Comp_ratio_old = worst_vsr
    # data_dict = {}
    # for i in range(len(worst_Comp_ratio_old)):
    #     data_dict[x_data_old[i]] = worst_Comp_ratio_old[i]
    # x_data =  ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    # worst_Comp_ratio= [data_dict[label] for label in x_data]
    # plt.plot(x_data, worst_Comp_ratio, color = "indigo", marker='o', ms=6,linestyle='--', label="WorstACRC", linewidth=3)


    plt.legend(fontsize = 18)
    # plt.title('CR & SU & VSR', fontsize=18)
    graph_path = os.path.dirname(os.path.abspath(__file__)) + '/graph/'
    if not os.path.isdir(graph_path):
        os.makedirs(graph_path)
    file_name = graph_path + __file__.split('/')[-1].split('.')[0]
    plt.yticks(fontsize=22)
    plt.xticks(fontsize=22,  rotation = 280)
    plt.savefig(f"{file_name}.pdf", bbox_inches='tight')
    plt.close()


if __name__ == '__main__':
    total_data = []
    for i in range(10):
        res = get_a_file_data(f'../../4_9_9_synchronous/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        # print(res)
    cr_data = np_total_data.mean(axis = 0).tolist()
    print("['MID', 'EDF', 'INS', 'SRPT', 'FB', 'THR', 'OPTFB', 'RM', 'SEDF', 'SALL', 'FIFO', 'FEDF']")
    print("Ave CR: ", cr_data)

    total_data = []
    for i in range(10):
        res = get_a_file_data(f'../../4_9_9_synchronous/test{i}/result_data.log', 'average us')
        total_data.append(res)
        np_total_data = np.array(total_data)
        # print(res)
    ave_us = np_total_data.mean(axis = 0).tolist()
    print("['MID', 'EDF', 'INS', 'SRPT', 'FB', 'THR', 'OPTFB', 'RM', 'SEDF', 'SALL', 'FIFO', 'FEDF']")
    print("Ave CRUR: ", ave_us)


    total_data = []
    for i in range(10):
        res = get_a_file_data(f'../../4_9_9_synchronous/test{i}/result_data.log', 'worst us')
        total_data.append(res)
        np_total_data = np.array(total_data)
        # print(res)
    worst_us = np_total_data.min(axis = 0).tolist()
    print("['MID', 'EDF', 'INS', 'SRPT', 'FB', 'THR', 'OPTFB', 'RM', 'SEDF', 'SALL', 'FIFO', 'FEDF']")
    print("Worst CRUR: ", worst_us)


    # total_data = []
    # for i in range(10):
    #     res = get_a_file_data(f'../../4_9_9_RandomOffsets/test{i}/result_data.log', 'average compratio')
    #     total_data.append(res)
    #     np_total_data = np.array(total_data)
    #     # print(res)
    # ave_vsr = np_total_data.mean(axis = 0).tolist()
    # print("['MID', 'EDF', 'INS', 'SRPT', 'FB', 'THR', 'OPTFB', 'RM', 'SEDF', 'SALL', 'FIFO', 'FEDF']")
    # print("Ave ACRC: ", ave_vsr)


    # total_data = []
    # for i in range(10):
    #     res = get_a_file_data(f'../../4_9_9_RandomOffsets/test{i}/result_data.log', 'worst compratio')
    #     total_data.append(res)
    #     np_total_data = np.array(total_data)
    #     # print(res)
    # worst_vsr = np_total_data.min(axis = 0).tolist()
    # print("['MID', 'EDF', 'INS', 'SRPT', 'FB', 'THR', 'OPTFB', 'RM', 'SEDF', 'SALL', 'FIFO', 'FEDF']")
    # print("Worst ACRC: ", worst_vsr)

    cr(cr_data,ave_us,worst_us)
        
